var loadTimer = null;
var loadingRes = {
    toLoad : 0, // Nombre de res à Load
    loaded : 0
};

var heroImg = addImgRes('Images/Farmer.png');
var heroEnemy = addImgRes('Images/EnemyFarmer.png');

//TILES & HEALTH STATES
var tile_enemy =    addImgRes('Images/Tiles/tile_enemy.png');
var tile_owned =    addImgRes('Images/Tiles/tile_owned.png');
var tile_neutral =  addImgRes('Images/Tiles/tile_neutral.png');
    var h_good =    addImgRes('Images/Tiles/Health/good.png');
    var h_medium =  addImgRes('Images/Tiles/Health/medium.png');
    var h_average = addImgRes('Images/Tiles/Health/average.png');
    var h_low =     addImgRes('Images/Tiles/Health/low.png');
    var h_bad =     addImgRes('Images/Tiles/Health/bad.png');

var dirtImg = addImgRes('Images/Tiles/dirt.png');

//BUILDINGS
var b_siloImg = addImgRes('Images/Buildings/building_silo.png');
    var g_siloImg_ok =      addImgRes('Images/Buildings/Ghosts/ghost_silo_ok.png');
    var g_siloImg_fail =    addImgRes('Images/Buildings/Ghosts/ghost_silo_fail.png');

var b_barnImg = addImgRes('Images/Buildings/building_barn.png');
    var g_barnImg_ok =      addImgRes('Images/Buildings/Ghosts/ghost_barn_ok.png');
    var g_barnImg_fail =    addImgRes('Images/Buildings/Ghosts/ghost_barn_fail.png');

var b_coldstorageImg = addImgRes('Images/Buildings/building_coldstorage.png');
    var g_coldstorageImg_ok =   addImgRes('Images/Buildings/Ghosts/ghost_coldstorage_ok.png');
    var g_coldstorageImg_fail = addImgRes('Images/Buildings/Ghosts/ghost_coldstorage_fail.png');

//CROPS
var c_decay = addImgRes('Images/Crops/decay.png');

var c_tomato_seededImg =            addImgRes('Images/Crops/Tomato/tomato_seeded.png');
    var c_tomato_seedlingsImg =     addImgRes('Images/Crops/Tomato/tomato_seedlings.png');
    var c_tomato_little_plantsImg = addImgRes('Images/Crops/Tomato/tomato_little_plants.png');
    var c_tomato_plantsImg =        addImgRes('Images/Crops/Tomato/tomato_plants.png');
    var c_tomato_mature_plantsImg = addImgRes('Images/Crops/Tomato/tomato_mature_plants.png');
        var g_tomato_ok =   addImgRes('Images/Crops/Ghosts/tomato_ok.png');
        var g_tomato_fail = addImgRes('Images/Crops/Ghosts/tomato_fail.png');

var c_corn_seededImg =            addImgRes('Images/Crops/Corn/corn_seeded.png');
    var c_corn_seedlingsImg =     addImgRes('Images/Crops/Corn/corn_seedlings.png');
    var c_corn_little_plantsImg = addImgRes('Images/Crops/Corn/corn_little_plants.png');
    var c_corn_plantsImg =        addImgRes('Images/Crops/Corn/corn_plants.png');
    var c_corn_mature_plantsImg = addImgRes('Images/Crops/Corn/corn_mature_plants.png');
        var g_corn_ok =   addImgRes('Images/Crops/Ghosts/corn_ok.png');
        var g_corn_fail = addImgRes('Images/Crops/Ghosts/corn_fail.png');

var c_wheat_seededImg =            addImgRes('Images/Crops/Wheat/wheat_seeded.png');
    var c_wheat_seedlingsImg =     addImgRes('Images/Crops/Wheat/wheat_seedlings.png');
    var c_wheat_little_plantsImg = addImgRes('Images/Crops/Wheat/wheat_little_plants.png');
    var c_wheat_plantsImg =        addImgRes('Images/Crops/Wheat/wheat_plants.png');
    var c_wheat_mature_plantsImg = addImgRes('Images/Crops/Wheat/wheat_mature_plants.png');
        var g_wheat_ok =   addImgRes('Images/Crops/Ghosts/wheat_ok.png');
        var g_wheat_fail = addImgRes('Images/Crops/Ghosts/wheat_fail.png');


//permet d'afficher la page
//seulement une fois toutes les ressources téléchargées par le client
function addImgRes(path) {
    var imgRes = new Image();

    imgRes.addEventListener('load',function() { loadingRes.loaded += 1; },false);
    imgRes.src = path;

    loadingRes.toLoad += 1;

    return imgRes;
}

function isImageLoaded(img) {
    if (!img.complete) {
        return false;
    }

    if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
        return false;
    }

    return true;
}

function loadRes() {
    loadTimer = setInterval(function () {
        if(loadingRes.loaded == loadingRes.toLoad) {
            clearInterval(loadTimer);
            console.log(loadingRes.toLoad + ' resources loaded successfully');
        }
        else {
            clearInterval(loadTimer);
            location.reload();
        }
    }, 1000);
}
